/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-11-04
 * Time: 13:14
 */

  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class Test {
    //快速幂
    public double myPow(double x, int n) {
        if (n < 0) return 1.0 / pow(x, -n);
        else return pow(x,n);
    }

    private double pow(double x, int n) {
        if (n == 0) return 1.0;
        double t = pow(x, n / 2);
        return n % 2 == 0 ? t * t : t * t * x;
    }

    public boolean evaluateTree(TreeNode root) {
        if (root.left == null && root.right == null) return root.val == 0 ? false : true;
        boolean l = evaluateTree(root.left);
        boolean r = evaluateTree(root.right);
        if (root.val  == 2) {
            return l | r;
        }else {
            return l & r;
        }
    }

    //求根节点到叶结点数字之和
    public int sumNumbers(TreeNode root) {
        return sumNumbersRoot(root, 0);
    }

    private int sumNumbersRoot(TreeNode root, int n) {
        if (root == null) return 0;
        n = n * 10 + root.val;
        if (root.left == null && root.right == null) return n;
        int l = sumNumbersRoot(root.left, n);
        int r = sumNumbersRoot(root.right, n);

        return l + r;
    }

    //二叉树剪枝
    public TreeNode pruneTree(TreeNode root) {
        if (root == null) return null;
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0) {
            root = null;
        }

        return root;
    }
}
